package com.xinke.module.system.service.social;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.social.*;
import com.xinke.module.system.model.entity.social.SocialUserBindDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.social.SocialUserBindMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 社交绑定 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class SocialUserBindServiceImpl implements SocialUserBindService {

    @Resource
    private SocialUserBindMapper socialUserBindMapper;

    @Override
    public Long createSocialUserBind(SocialUserBindSaveReqVO createReqVO) {
        // 插入
        SocialUserBindDO socialUserBind = BeanUtils.toBean(createReqVO, SocialUserBindDO.class);
        socialUserBindMapper.insert(socialUserBind);
        // 返回
        return socialUserBind.getBindId();
    }

    @Override
    public void updateSocialUserBind(SocialUserBindSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialUserBindExists(updateReqVO.getBindId());
        // 更新
        SocialUserBindDO updateObj = BeanUtils.toBean(updateReqVO, SocialUserBindDO.class);
        socialUserBindMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialUserBind(Long bindId) {
        // 校验存在
        validateSocialUserBindExists(bindId);
        // 删除
        socialUserBindMapper.deleteById(bindId);
    }

    private void validateSocialUserBindExists(Long bindId) {
        if (socialUserBindMapper.selectById(bindId) == null) {
            throw exception(null);
        }
    }

    @Override
    public SocialUserBindDO getSocialUserBind(Long bindId) {
        return socialUserBindMapper.selectById(bindId);
    }

    @Override
    public PageResult<SocialUserBindDO> getSocialUserBindPage(SocialUserBindPageReqVO pageReqVO) {
        return socialUserBindMapper.selectPage(pageReqVO);
    }

}